import pygame
import sys
import os



# 初始化pygame
pygame.init()

# 设置屏幕尺寸和背景图片路径
screen_size = (800, 600)
background_image_path = "background.jpg"
background_image_path = "background.gif"
background_image_path = "img_sky.png"
SCREENRECT = pygame.Rect(0, 0, 800, 600)


main_dir = os.path.split(os.path.abspath(__file__))[0]

# 对图片进行缩放处理
# scale_factor = 0.1  # 缩放因子，可以根据需要进行调整
# bee_image = pygame.image.load(bee_image)
# background_image = pygame.transform.scale(background_image, (int(800), int(600)))
x=0
# 加载背景图片
def load_image(file,scale_factor):
    if scale_factor == None:
        scale_factor = 1
    """loads an image, prepares it for play"""
    file = os.path.join(main_dir, "data", file)
    try:
        surface = pygame.image.load(file)
        print(surface.get_width(),surface.get_height(),surface.get_size(),surface.get_size()*10)
        # 图片进行缩放处理
        # surface = pygame.transform.scale(surface, (int(800), int(600)))
        surface = pygame.transform.scale(surface, tuple(x*scale_factor for x in surface.get_size()))
    except pygame.error:
        raise SystemExit(f'Could not load image "{file}" {pygame.get_error()}')
    return surface.convert()


# 创建游戏窗口
screen = pygame.display.set_mode(screen_size)
print(screen.get_size())
print(screen.get_width())
print(screen.get_height())
# 游戏主循环
while True:
    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    background_image = load_image(background_image_path,1)
    background = pygame.Surface(SCREENRECT.size)
    x+=0.1
    background.blit(background_image,(x,0))
    # for x in range(0,SCREENRECT.width,background_image.get_width()):
    #     background.blit(background_image,(x,0))
    #     for y in range(0,SCREENRECT.width,background_image.get_height()):
    #         background.blit(background_image,(x,y))

    # 将背景图片绘制到游戏窗口上
    screen.blit(background, (0, 0))

    # 更新屏幕显示
    pygame.display.flip()
